import re
import requests
from bs4 import BeautifulSoup
import os
from selenium import webdriver
import queue
import time
from selenium.webdriver.chrome.options import Options


def get(url):
    ch_options= Options()
    ch_options.add_argument('--headless')
    try:
        #driver = webdriver.Chrome(executable_path="D:\\pythonglr\python\\driver\\chromedriver",chrome_options= ch_options)
        driver = webdriver.Chrome(executable_path="D:\\pythonglr\python\\driver\\chromedriver")
        driver.get(url)
        html= driver.page_source
        #time.sleep(120)
        driver.close

        return html
    except Exception as e:
        print(1,3)
        return ''


index_url = "https://app.finance.ifeng.com/list/stock.php?t=hs"
url_dict= {}
index_pagesource= get(index_url)
soup= BeautifulSoup(index_pagesource,'html.parser')

table= soup.table
#print(1,table)
trs= table.find_all('tr')
#print(1,trs)
for tr in trs:
    stock_code= tr.find('a').string#找到tr中的第一个标签
    #print(1,stock_code)
    if 'href' in tr.find('a').attrs.keys():
        stock_url= tr.find('a').attrs['href']
        #print(1,stock_code,stock_url)
    if re.search(r'^\d+$',stock_code):
        url_dict[stock_code]= stock_url
print(url_dict)




all_stock_info= []
for url in list(url_dict.values())[:2]:
    #print(url)
    page_source = get(url)
    soup = BeautifulSoup(page_source, 'html.parser')

    table = soup.table
    tables = soup.find_all('table')
    info_table = ''
    name_table = ''

    for table in tables:
        if 'class' in table.attrs.keys() and table.attrs['class'] == ['tabNum']:
            # print(1,table)
            info_table = table
        elif 'class' in table.attrs.keys() and table.attrs['class'] == ['tabPic']:
            # print(1,table)
            name_table = table
            print(111)

    stock_info = {}

    trs = info_table.find_all('tr')
    for tr in trs:
        # print(3,tr)
        tds = tr.find_all('td')
        # print(2,tds)
        for td in tds:
            # print(2,td.text)
            key = td.text.split('：')[0].replace(' ', '').replace('\u3000', '')
            value = td.text.split('：')[1]
            stock_info[key] = value
    #print(1, stock_info)
    print(1, stock_info)

    stock_name = name_table.find('td').text.strip()  # 获取第一个标签td
    print(2, stock_name)
    if '(' in stock_name:
        stock_code = stock_name.split('(')[1][:-1]
        stock_name = stock_name.split('(')[0]
        stock_info['股票名字'] = stock_name
        stock_info['股票代码'] = stock_code
    print(2, stock_info)
    all_stock_info.append(stock_info)

with open('D:\\test\\stockinfo.txt','w',encoding='utf-8') as fp:
    for i in all_stock_info:
        fp.write(str(i)+'\n')


